﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.Drawing;
using Microsoft.VisualStudio.Modeling;
using System.IO;

namespace DSLFactory.VSCTDesigner
{
  
    partial class BitmapShape
    {
        class DynamicBitmapShapeGeometry : RectangleShapeGeometry
        {
            private BitmapShape _parentShape;

            public DynamicBitmapShapeGeometry(BitmapShape parentShape)
            {
                _parentShape = parentShape;
            }

            protected override void DoPaintGeometry(DiagramPaintEventArgs e, IGeometryHost geometryHost)
            {
                Graphics graphics = e.Graphics;
                RectangleD geometryBoundingBox = geometryHost.GeometryBoundingBox;
                Image img = null;
                Bitmap bmp = _parentShape.ModelElement as Bitmap;
                if( bmp != null )
                {
                    img = bmp.ResolveImage(null);
                }

                if( img != null )
                    graphics.DrawImage(img, RectangleD.ToRectangleF(geometryBoundingBox));
                else
                {
                    using( Brush b = new SolidBrush(Color.Gainsboro) )
                    {
                        graphics.FillRectangle(b, RectangleD.ToRectangleF(geometryBoundingBox));
                    }
                }
            }
        }

        private DynamicBitmapShapeGeometry _shapeGeometry;

        public override Microsoft.VisualStudio.Modeling.Diagrams.ShapeGeometry ShapeGeometry
        {
            get
            {
                if( _shapeGeometry == null )
                {
                    _shapeGeometry = new DynamicBitmapShapeGeometry(this);
                    FitToContent();
                }
                return _shapeGeometry;
            }
        }

        protected override void InitializeResources(Microsoft.VisualStudio.Modeling.Diagrams.StyleSet classStyleSet)
        {
            base.InitializeResources(classStyleSet);
            AssociateValueWith(this.Store, Bitmap.hRefDomainPropertyId);
            AssociateValueWith(this.Store, Bitmap.resIdDomainPropertyId);
            FitToContent();
        }

        protected override void OnAssociatedPropertyChanged(PropertyChangedEventArgs e)
        {
            base.OnAssociatedPropertyChanged(e);
            FitToContent();
            Invalidate(true);
        }

        private void FitToContent()
        {
            using( Transaction transaction = this.Store.TransactionManager.BeginTransaction("Resize shape") )
            {
                this.AbsoluteBounds = new RectangleD(this.Location, DefaultSize);
                transaction.Commit();
            }
        }

        public override SizeD DefaultSize
        {
            get
            {
                if( this.ModelElement != null )
                {
                    Image img = ( (Bitmap)this.ModelElement ).ResolveImage(null);
                    if( img != null )
                    {
                        SizeF imageSize = ImageHelper.GetImageSize(img);
                        return new SizeD((double)imageSize.Width, (double)imageSize.Height);
                    }
                }
                return new SizeD(0.4, 0.2);
            }
        }

        public override bool HasConnectionPoints
        {
            get
            {
                return true;
            }
        }

        public override void EnsureConnectionPoints(Microsoft.VisualStudio.Modeling.Diagrams.LinkShape link)
        {
            CreateConnectionPoint(new Microsoft.VisualStudio.Modeling.Diagrams.PointD(this.AbsoluteBoundingBox.Center.X,
                this.AbsoluteBoundingBox.Bottom));
        }
    }
}
